﻿using DBUtil.FastEntity.Annotations;
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;

namespace DBUtil.FastEntity.Test
{
    //用户: 用户详情 => 1:1
    namespace Demo_One2One_1
    {
        [Table("t_user")]
        public class UserEntity
        {
            [PrimaryKey]
            [Column("id")]
            public int Id { get; set; }

            [Column("name")]
            public string Name { get; set; }

            public UserDetailEntity Detail { get; set; }
        }

        [Table("t_userdetail")]
        public class UserDetailEntity
        {
            [PrimaryKey]
            [Column("id")]
            public int Id { get; set; }

            [ReferHit(ReferTable = typeof(UserEntity))]
            [Column("userid")]
            public int UserId { get; set; }

            [Column("age")]
            public int Age { get; set; }
        }
    }

    //表单,CreateUserId/UpdateUserId: User
    namespace Demo_One2One_2
    {
        [Table("t_user")]
        public class UserEntity
        {
            [PrimaryKey]
            [Column("id")]
            public int Id { get; set; }

            [Column("name")]
            public string Name { get; set; }
        }

        [Table("t_form")]
        public class FormEntity
        {
            [PrimaryKey]
            [Column("id")]
            public int Id { get; set; }

            [ReferHit(ReferName = nameof(CreateUserId), ReferTable = typeof(UserEntity))]
            [Column("createuserid")]
            public int CreateUserId { get; set; }

            [ReferTable(ReferHint = nameof(CreateUserId))]
            public UserEntity CreateUser { get; set; }

            [ReferHit(ReferName = nameof(UpdateUserId), ReferTable = typeof(UserEntity))]
            [Column("updateuserid")]
            public int UpdateUserId { get; set; }

            [ReferTable(ReferHint = nameof(UpdateUserId))]
            public UserEntity UpdateUser { get; set; }
        }
    }

    //引用自身: User/LeaderId
    namespace Demo_One2One_3
    {
        [Table("t_user")]
        public class UserEntity
        {
            [PrimaryKey]
            [Column("id")]
            public int Id { get; set; }

            [Column("name")]
            public string Name { get; set; }

            [ReferHit(ReferTable = typeof(UserEntity))]
            public int LeaderId { get; set; }

            public UserEntity Leader { get; set; }
        }
    }

    //数据字典,form.areacode=dict.code and dict.category='areacode'
    namespace Demo_One2One_4
    {
        [Table("t_form")]
        public class Form
        {
            [PrimaryKey]
            public int Id { get; set; }
            public string Name { get; set; }

            /* AreaCode */
            [ReferHit(ReferName = nameof(AreaCode),
                ReferTable = typeof(Dict),
                ReferProp = nameof(Dict.Code),
                StaticReferProp1 = nameof(Dict.Category), StaticReferValue1 = "AreaCode")]
            public string AreaCode { get; set; }

            [ReferColumn(ReferHint = nameof(AreaCode), ReferProp = nameof(Dict.Name))]
            public string AreaName { get; set; }

            [ReferTable(ReferHint = nameof(AreaCode))]
            public Dict AreaDict { get; set; }

            /* ClassCode */
            [ReferHit(ReferName = nameof(ClassCode),
                ReferTable = typeof(Dict),
                ReferProp = nameof(Dict.Code),
                StaticReferProp1 = nameof(Dict.Category), StaticReferValue1 = "ClassCode")]
            public string ClassCode { get; set; }

            [ReferColumn(ReferHint = nameof(ClassCode), ReferProp = nameof(Dict.Name))]
            public string ClassName { get; set; }

            [ReferTable(ReferHint = nameof(ClassCode))]
            public Dict ClassDict { get; set; }
        }

        public class Dict
        {
            [PrimaryKey]
            public int Id { get; set; }
            public string Name { get; set; }
            public string Code { get; set; }
            public string Category { get; set; }
        }
    }

    //数组字典,form.areacode=dict.code and form.category=dict.category
    namespace Demo_One2One2_5
    {
        public class Form
        {
            [PrimaryKey]
            public int Id { get; set; }
            public string Name { get; set; }

            /*Area从字典*/
            [ReferHit(ReferName = nameof(AreaCode),
                ReferTable = typeof(Dict),
                ReferProp = nameof(Dict.Code))]
            public string AreaCode { get; set; }

            [ReferHit(ReferName = nameof(AreaCode),
                ReferTable = typeof(Dict),
                ReferProp = nameof(Dict.Category))]
            public string AreaCategory { get; set; }

            [ReferColumn(ReferHint = nameof(AreaCode), ReferProp = nameof(Dict.Name))]
            public string AreaName { get; set; }

            /*Class从字典*/
            [ReferHit(ReferName = nameof(ClassCode),
                ReferTable = typeof(Dict),
                ReferProp = nameof(Dict.Code))]
            public string ClassCode { get; set; }

            [ReferHit(ReferName = nameof(ClassCode),
                ReferTable = typeof(Dict),
                ReferProp = nameof(Dict.Category))]
            public string ClassCategory { get; set; }


            [ReferColumn(ReferHint = nameof(AreaCode), ReferProp = nameof(Dict.Name))]
            public string ClassName { get; set; }
        }

        public class Dict
        {
            [PrimaryKey]
            public int Id { get; set; }
            public string Name { get; set; }
            public string Code { get; set; }
            public string Category { get; set; }
        }
    }

    //1对多 Teacher/Student TeacherId
    namespace Demo_One2Many_1
    {
        [Table("t_teacher")]
        public class Teacher
        {
            [PrimaryKey]
            public int Id { get; set; }
            public string Name { set; get; }

            public List<Student> Students { get; set; }
        }

        [Table("t_student")]
        public class Student
        {
            [PrimaryKey]
            public int Id { get; set; }
            public string Name { get; set; }

            [ReferHit(ReferTable = typeof(Teacher))]
            public int TeacherId { get; set; }

            public Teacher Teacher { get; set; }
        }
    }

    //1对多 Teacher/Student StudentIds
    namespace Demo_One2Many_2
    {
        [Table("t_teacher")]
        public class Teacher
        {
            [PrimaryKey]
            public int Id { get; set; }
            public string Name { set; get; }

            [ReferHit(ReferTable = typeof(Student), ReferProp = nameof(Student.Id), ReferHitType = EnumReferHitType.Split, SplitString = ",")]
            public string StudentIds { get; set; }

            public List<Student> Students { get; set; }
        }

        [Table("t_student")]
        public class Student
        {
            [PrimaryKey]
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }

    //1对多 Teacher/Student StudentIds
    namespace Demo_One2Many_3
    {
        [Table("t_log")]
        public class Log
        {
            [PrimaryKey]
            public int Id { get; set; }

            [ReferHit(ReferTable = typeof(Dialog), StaticThisValue = "Dialog")]
            [ReferHit(ReferTable = typeof(Pop), StaticThisValue = "Dialog")]
            public string LogType { get; set; }

            [ReferHit(ReferTable = typeof(Dialog), ReferProp = nameof(Dialog.Id))]
            [ReferHit(ReferTable = typeof(Pop), ReferProp = nameof(Pop.Id))]
            public int ObjectId { get; set; }

            public string Message { get; set; }
        }

        public class Dialog
        {
            [PrimaryKey]
            public int Id { get; set; }
            public string Name { get; set; }

            public List<Log> Logs { get; set; }
        }

        public class Pop
        {
            [PrimaryKey]
            public int Id { get; set; }
            public string Name { get; set; }

            public List<Log> Logs { get; set; }
        }
    }

    // 多对多 关联表
    namespace Demo_Many2Many
    {
        public class Student
        {
            [PrimaryKey]
            public int Id { get; set; }
            public string Name { set; get; }

            [UseReferHitTable(HitTable = typeof(StudentBook))]
            public List<Book> Books { get; set; }
        }

        public class Book
        {
            [PrimaryKey]
            public int Id { get; set; }
            public string Name { get; set; }

            [UseReferHitTable(HitTable = typeof(StudentBook))]
            public List<Student> Students { get; set; }
        }

        public class StudentBook
        {
            [PrimaryKey]
            public int Id { get; set; }

            [ReferHit(ReferTable = typeof(Student))]
            public int StudentId { get; set; }

            [ReferHit(ReferTable = typeof(Book))]
            public int BookId { get; set; }
        }
    }

    #region 树 三种模式
    // 树 id-parentid
    namespace Demo_Tree_1
    {
        [TableTree(TreeType = EnumTableTreeType.ParentId)]
        [Table("t_category")]
        public class Category
        {
            [PrimaryKey]
            [Column("id")]
            public int Id { get; set; }

            [Column("name")]
            public string Name { get; set; }

            [ReferHit(ReferTable = typeof(Category))]
            [Column("parentid")]
            public int ParentId { get; set; }

            public List<Category> Children { get; set; }
        }
    }

    //树 id-parentid
    namespace Demo_Tree_2
    {
        [TableTree(TreeType = EnumTableTreeType.ParentId)]
        [Table("t_category")]
        public class SystemArea
        {
            [PrimaryKey]
            [Column("id")]
            public int Id { get; set; }

            [ReferHit(ReferTable = typeof(SystemArea))]
            public int ParentId { get; set; }

            [Column("name")]
            public string Name { get; set; }

            public List<SystemArea> Children { get; set; }
        }

        //精确定位类型

        [TableTree(TreeType = EnumTableTreeType.ParentId)]
        [Table("t_category")]
        public class SystemArea2
        {
            [PrimaryKey]
            [Column("id")]
            public int Id { get; set; }

            [TableTreeId]
            [Column("areacode")]
            public string AreaCode { get; set; }

            [Column("name")]
            public string Name { get; set; }

            [TableTreeParentId]
            public string ParentCode { get; set; }

            [TableTreeChildren]
            public List<SystemArea> Children { get; set; }
        }
    }

    //树 SqlHierarchyId
    namespace Demo_Tree_3
    {
        [TableTree(TreeType = EnumTableTreeType.HierarchyId)]
        [Table("t_category")]
        public class CategoryEntity
        {
            [PrimaryKey]
            [Column("id")]
            public int Id { get; set; }

            [Column("name")]
            public string Name { get; set; }

            //[Column("hierarchy")]
            //public SqlHierarchyId Hierarchy { get; set; }

            [TableTreeChildren]
            public List<CategoryEntity> Children { get; set; }
        }
    }

    //树 idSegment Code="1-2-25"
    namespace Demo_Tree_4
    {
        [TableTree(TreeType = EnumTableTreeType.IdSegment)]
        [Table("t_category")]
        public class CategoryEntity
        {
            [PrimaryKey]
            [Column("id")]
            public int Id { get; set; }

            [Column("name")]
            public string Name { get; set; }


            [TableTreeIdSegment(SplitChar = "-")]
            [Column("code")]
            public string Code { get; set; }
        }
    }
    #endregion
}
